<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/14 0014
 * Time: 下午 2:00
 */

namespace app\admin\controller;

use app\admin\model\Admin;
use think\Controller;
use think\Request;
use think\Session;

class Login extends Controller
{
    /**
     * @title   登陆操作
     * @return mixed
     */
    public function index()
    {
        if(Session::has('adminid')) $this->redirect('index/index');
        if(Request::instance()->isAjax() && Request::instance()->isPost()){
            if(!captcha_check(input('post.code'))){
                //验证失败
                $this->error('验证码错误');
            };
            $admin = new Admin();
            $find = $admin::get(['account'=>input('post.account'),'pwd'=>md5(input('post.password'))]);
            if($find==null){
                $this->error('账号或密码错误');
            } else{
                if($find['state'] != 1){
                    $this->error('该管理员已被禁用，请联系程序员解锁');
                }
                Session::set('adminid',$find['id']);
                Session::set('gid',$find['gid']);
                Session::set('account',$find['account']);
                Session::set('head',$find['head']);
                $this->success('登陆成功','index/index');
            }
        }
        return $this->fetch('admin/login/login');
    }

    public function loginout()
    {
        Session::delete('adminid');
        $this->redirect('login/index');
    }
}